package com.springboot.security.service;

import com.springboot.security.entity.User;
import com.springboot.security.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author wangning
 * @create 2021-05-18 17:42
 */
@Service
public class UserService implements UserDetailsService {
	@Autowired
	private UserMapper userMapper;
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		User user = userMapper.loadUserByUsername(username);
		if (user == null) {
			throw new UsernameNotFoundException("账户不存在");
		}
		user.setRoles(userMapper.getUserRoleByUid(user.getId()));
		System.out.println(userMapper.getUserRoleByUid(user.getId()));

		return user;
	}
}
